package com.hiwan.system.service.file;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.framework.dao.annotation.LogRecord;
import com.hiwan.system.common.base.GenericService;
import com.hiwan.system.dao.file.FileInfoDao;
import com.hiwan.system.pojo.FileInfo;

/**
 * 
 * Datetime ： 2015-10-28 下午3:37:50<br>
 * Title : FilesInfoService.java<br>
 * Description: 文件业务处理类 <br>
 * Copyright : Copyright (c) 2013<br>
 * Company : HIWAN<br>
 * 
 * @author llg
 * 
 */
@Service
@Transactional
public class FileInfoService extends GenericService<FileInfoDao, FileInfo, String>{
	
	
	@Autowired
	private FileService fileService;

	/**
	 * 查询文件信息列表(不分页)
	 * 
	 * @param fileInfo
	 * @return
	 */
	public List<FileInfo> selectFilesInfoList(String fileId, String attachId) {
		List<FileInfo> files = dao.selectFileInfoList(fileId, attachId);
		String url = null;
		for (FileInfo file : files) {
			file.setFilePath(url);
			file.setTfilePath(url);
		}
		return files;
	}

	public List<FileInfo> selectFilesInfoList(String attachId) {
		List<FileInfo> files = dao.selectFileInfoList(null, attachId);
		return files;
	}

	/**
	 * 查询文件信息
	 * 
	 * @param fileId
	 * @return
	 */
	public FileInfo selectFilesInfoById(String fileId, String attachId) {
		return dao.selectFileInfoById(fileId, attachId);
	}

	/**
	 * 插入文件信息
	 * 
	 * @param fileInfo
	 * @return
	 * @throws SQLException
	 */
	@LogRecord
	public int insertFilesInfo(FileInfo fileInfo) throws SQLException {
		Integer fileOrder = dao.getNextFileOrder(fileInfo.getAttachId());
		if(fileOrder == null){
			fileOrder = 0;
		}
		fileInfo.setFileOrder(fileOrder);
		fileInfo.setFileId(com.hiwan.platform.utils.StringUtils.getUUID());
		FileInfo file = dao.selectFileInfoById(fileInfo.getFileId(),
				fileInfo.getAttachId());
		
		if (file != null) {
			if (file.equals(fileInfo)) {
				return 1;
			} else {
				return dao.update(fileInfo);
			}

		} else {
			return dao.insert(fileInfo);
		}

	}
	

	/**
	 * 删除文件信息
	 * 
	 * @param fileId
	 * @return
	 */
	@LogRecord
	public int deleteFilesInfoByAttachId(String attachId) {
		List<FileInfo> files = selectFilesInfoList(attachId);
		for (FileInfo file : files) {
			fileService
					.deleteFile(file, "1");
		}
		return dao.delete(new FileInfo("",attachId));
	}
	
	public String saveFile(String orgId, String[] fileNames) {
		Long uuid = UUID.randomUUID().getMostSignificantBits();
		if (uuid < 0) {
			uuid = -1 * uuid;
		}

		FileInfo file = new FileInfo();
		file.setAttachId(Long.toHexString(uuid));
		file.setBelongOrg(orgId);
		for (int i = 1; i <= fileNames.length; i++) {
			file.setFileId(String.valueOf(i));
			file.setFileName(fileNames[i - 1]);
			dao.insert(file);
			if (fileService.saveTempFile(orgId, fileNames[i - 1]) == false) {
				throw new RuntimeException("文件保存到正式目录失败！");
			}
		}

		return file.getAttachId();
	}
	
	@LogRecord
	public boolean saveFile(String orgId, List<String> fileNames, String AttachId) throws SQLException {
		FileInfo file = new FileInfo();
		file.setAttachId(AttachId);
		file.setBelongOrg(orgId);
		for (int i = 1; i <= fileNames.size(); i++) {
			file.setFileName(fileNames.get(i));
			insertFilesInfo(file);
			if (fileService.saveTempFile(orgId, fileNames.get(i)) == false) {
				throw new RuntimeException("文件保存到正式目录失败！");
			}
		}

		return true;
	}
	public int dragingFileOrder(String attachId, String dragIndex, String targetIndex){
		FileInfo dragFile = dao.selectFileInfoByOrder(attachId, dragIndex);
		FileInfo targetFile = dao.selectFileInfoByOrder(attachId, targetIndex);
		int targetOrder = targetFile.getFileOrder();
		int dragingOrder = dragFile.getFileOrder();
		int step = 1, maxFileOrder = dragingOrder - 1, minFileOrder = targetOrder;
		if(targetOrder > dragingOrder){
			step = -1;
			maxFileOrder = targetOrder;
			minFileOrder = dragingOrder + 1;
		}
		
		dao.updateOtherOrder(attachId, maxFileOrder, minFileOrder, step);
		dao.updateFileOrder(attachId, dragFile.getFileId(), targetOrder);
		
		return 0;
	}
}
